//
//  ListViewController.m
//  UI__相册三代版
//
//  Created by dllo on 16/4/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ListViewController.h"

@interface ListViewController ()

@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *myArray;
@property(nonatomic,retain)NSMutableDictionary *myDic;
@property(nonatomic,retain)NSMutableArray *keyArray;


@end

@implementation ListViewController

-(void)dealloc{
    [_myArray release];
    [_myTableView release];
    [_myDic release];
    [_keyArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    
    //想用必须调用
    [self getData];
    
    self.navigationItem.title = @"列表页";
    //头上加点字
    
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    
    //注册cell
   [ self.myTableView registerClass:[ListTableViewCell class ]forCellReuseIdentifier:@"reuse"];
    
    //取消线
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //创建一个右上角的按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightAction:)];
    

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailsViewController *detailsVC = [[DetailsViewController alloc]init];
    
    NSString *key = [self.keyArray objectAtIndex:indexPath.section];
    NSMutableArray *array = [self.myDic objectForKey:key];
    ListModel *model1 = array[indexPath.row];
    detailsVC.model = model1;
    detailsVC.index = indexPath;
    
    detailsVC.myBlock = ^(ListModel *model,NSIndexPath *index){
        NSString *string = [[ChineseToPinyin pinyinFromChiniseString:model.name]substringToIndex:1];
        NSString *keyString = [self.keyArray objectAtIndex:index.section];
        if ([string isEqualToString:keyString]) {
            
            NSMutableArray *isArray = [self.myDic objectForKey:string];
            ListModel *YuanLaiModel = [isArray objectAtIndex:index.row];
            YuanLaiModel.image = model.image;
            YuanLaiModel.name = model.name;
            YuanLaiModel.number = model.number;
            
        }else{
         NSMutableArray *isArray = [self.myDic objectForKey:string];
            [isArray removeObjectAtIndex:index.row];
            if ([self.keyArray containsObject:string]) {
                [isArray addObject:model];
                
                
            }else{
                NSMutableArray *newArray = [NSMutableArray arrayWithObjects:model, nil];
                [self.myDic setObject:newArray forKey:string];
                [self.keyArray addObject:string];
            
            }
        
        }
        [self.myTableView reloadData];
    
    };
    
    [self.navigationController pushViewController:detailsVC animated:YES];
    [detailsVC release];

}

//右按钮
-(void)rightAction:(UIBarButtonItem *)item{
    AddViewController *addVC = [[AddViewController alloc]init];
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:addVC];
    [addVC release];
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    [naVC release];
    
    addVC.myBlock = ^(ListModel *model){
    
        NSString *string = [[ChineseToPinyin pinyinFromChiniseString:model.name]substringToIndex:1];
        //看看数组有没有string
        if ([self.keyArray containsObject:string]) {
            NSMutableArray *array = [self.myDic objectForKey:string];
            
            [array addObject:model];
        }else{
        
            NSMutableArray *array = [NSMutableArray array];
            [array addObject:model];//(为啥不释放)
            [self.myDic setObject:array forKey:string];
            [self.keyArray addObject:string];
            [self.keyArray sortUsingSelector:@selector(compare:)];
        }
        
        [self.myTableView reloadData];
    
    };




}




//写着创建一个方法
-(void)getData{
    //大字典初始化
    self.myDic = [NSMutableDictionary dictionary];
    
    
    ListModel *model0 = [[ListModel alloc]init];
    model0.name = @"春雨老师";
    model0.number = @"0508";
    model0.image = [UIImage imageNamed:@"q0.jpg"];
    NSMutableArray *CArray = [NSMutableArray arrayWithObjects:model0, nil];
    [model0 release];
    [self.myDic setObject:CArray forKey:@"C"];

    
    
    
    ListModel *model1 = [[ListModel alloc]init];
    model1.name = @"唐嫣";
    model1.number = @"123456";
    model1.image = [UIImage imageNamed:@"q1.jpeg"];
    NSMutableArray *TArray = [NSMutableArray arrayWithObjects:model1, nil];
    [model1 release];
    
    [self.myDic setObject:TArray forKey:@"T"];

    
    ListModel *model2 = [[ListModel alloc]init];
    model2.name = @"邓肯";
    model2.number = @"21";
    model2.image = [UIImage imageNamed:@"q2.jpeg"];
    NSMutableArray *DArray = [NSMutableArray arrayWithObjects:model2, nil];
    [model2 release];
    
    [self.myDic setObject:DArray forKey:@"D"];

    
    
    ListModel *model3 = [[ListModel alloc]init];
    model3.name = @"小黑人";
    model3.number = @"33333";
    model3.image = [UIImage imageNamed:@"q5.jpg"];
    
    ListModel *model4 = [[ListModel alloc]init];
    model4.name = @"小胖子";
    model4.number = @"10086";
    model4.image = [UIImage imageNamed:@"q4.jpg"];
    //都是一个姓的放一个字典里 OC学的
    NSMutableArray *XArray = [NSMutableArray arrayWithObjects:model3, model4, nil];
    [model3 release];
    [model4 release];
    //把x放到字典里
    [self.myDic setObject:XArray forKey:@"X"];
    
    ListModel *model5 = [[ListModel alloc]init];
    model5.name = @"博尔特";
    model5.number = @"2008";
    model5.image = [UIImage imageNamed:@"q3.jpg"];
    NSMutableArray *BArray = [NSMutableArray arrayWithObjects:model5, nil];
    [model5 release];
    [self.myDic setObject:BArray forKey:@"B"];
    
    
    
    
    
    
    
    
    //在初始化的同时将另一个数组的值给我们新创建的数组.
    self.keyArray = [NSMutableArray arrayWithArray:self.myDic.allKeys];
   
    //可变数组排序
    [self.keyArray sortUsingSelector:@selector(compare:)];
    
}

//删除功能(三步)
//(不想删除的拿出来no)
//(全部访问1)
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
}
//(必须2)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
          //删除的意思
    return UITableViewCellEditingStyleDelete;
}
//(必须三)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //保护一下
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //拿到对应section的key值
        NSString *key = [self.keyArray objectAtIndex:indexPath.section];
        
        //拿到对应section的array
        NSMutableArray *array = [self.myDic objectForKey:key];
        
        //删除数组对应的数据
        [array removeObjectAtIndex:indexPath.row];
        
        if (array.count == 0) {
           
            //如果分区下的数组为空,那么我们就删除这个分区
            [self.keyArray removeObjectAtIndex:indexPath.section];
            
            
        }
        //刷新tableView(必须刷新一遍)
        [self.myTableView reloadData];
    }

}






//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.keyArray.count;
}

//分区标题内容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [self.keyArray objectAtIndex:section];
}




//必须实现方法之一

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString *key = [self.keyArray objectAtIndex:section];
    
    NSMutableArray *array = [self.myDic objectForKey:key];
    return array.count;
}


//必须实现方法之二
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    //把section的key值拿出来
    NSString *key = [self.keyArray objectAtIndex:indexPath.section];
   
    //通过key找对应的array
    NSMutableArray *array = [self.myDic objectForKey:key];
    ListModel *model = [array objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    //取消点击效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //加个1111  cell.textLabel.text = @"11111";
 //   cell.textLabel.text = model.name;
    return cell;
    
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 150;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
